package day_2022_9_to_12.Day;

/**
 * @author haomin
 * @date 2022/09/08 21:17
 **/
public class Day6_10_Num152_MaxProduct {
    class Solution {
        public int maxProduct(int[] nums) {
            int maxVal = 0, pro = 1;
            // 从左到右相乘，寻最大值
            for(int i = 0; i < nums.length; ++i){
                pro *= nums[i];
                maxVal = Math.max(maxVal, pro);
                if(nums[i] == 0) pro = 1;
            }
            pro = 1;
            // 从右到左相乘，寻最大值
            for(int i = nums.length-1; i >= 0; --i){
                pro *= nums[i];
                maxVal = Math.max(maxVal, pro);
                if(nums[i] == 0) pro = 1;
            }
            return maxVal;
        }
    }
}